package com.dap.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangmin
 * @discription
 * @created 2020-06-04 15:14
 */
public abstract class ListUtil {
    /**
     * 根据 pageSize 等分 list
     * */
    public static <T> List<List<T>> divideByPageSize(final List<T> list, final int pageSize) {
        ArrayList<List<T>> result = new ArrayList<>();
        int idx = 0;
        while (idx < list.size()) {
            ArrayList<T> element = new ArrayList<>();
            result.add(element);

            final int endIdx = Math.min(idx + pageSize, list.size());

            for (; idx < endIdx; ++idx) {
                element.add(list.get(idx));
            }
        }

        return result;
    }

    public static List<Long> strToLongList(String str, String delimiter) {
        ArrayList<Long> result = new ArrayList<>();

        int i = 0;
        while (i < str.length()) {
            int nextIdx = str.indexOf(delimiter, i);
            if (nextIdx < 0) {
                nextIdx = str.length();
            }
            if (i < nextIdx) {
                result.add(Long.parseLong(str.substring(i, nextIdx)));
            }
            i = nextIdx + delimiter.length();
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println(strToLongList("904", ","));
    }
}
